package com.boshao.classschedule.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.activity.MainActivity;
import com.boshao.classschedule.fragment.AddOrModifyClassFragment;
import com.boshao.classschedule.fragment.ClassScheduleFragment;
import com.boshao.classschedule.model.classschedule.ClassDaoMaster;
import com.boshao.classschedule.model.classschedule.ClassDaoMaster.ClassDevOpenHelper;
import com.boshao.classschedule.model.classschedule.ClassDaoSession;
import com.boshao.classschedule.model.classschedule.ClassSchedule;
import com.boshao.classschedule.model.classschedule.ClassScheduleDao;
import com.boshao.classschedule.model.classschedule.ClassScheduleDao.Properties;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * 课程表框Adapter
 * @author boshao
 *
 */
public class ClassGridAdapter extends RecyclerView.Adapter<ClassGridAdapter.ViewHolder>{
	
	public static final String[] SCHEDULETIMES = {"8:00","9:05","10:10","11:05","14:00",
		"15:20","16:20","17:10","19:30","20:25","",""};
	private Context mContext;
	public List<ViewHolder> mViewHolders = new ArrayList<>();
	private OnItemClickListener mOnItemClickListener;
	public static final String[] WEEKDAYS = {"周一","周二","周三","周四","周五","周六","周日"};
	private int mCurrentWeek;
	
	private ClassScheduleFragment mClassScheduleFragment;
	public void setCurrentWeek(int week){
		mCurrentWeek = week;
	}
	
	public ClassGridAdapter(Context context,ClassScheduleFragment classScheduleFragment) {
		mOnItemClickListener = new GridOnItemClickListener();
		mClassScheduleFragment = classScheduleFragment;
		mContext = context;
	}

	@Override
	public int getItemCount() {
		return 12;
	}

	@Override
	public void onBindViewHolder(ViewHolder viewHolder, int position) {
		viewHolder.mTvTime.setText(SCHEDULETIMES[position]);
		viewHolder.mTvNum.setText(String.valueOf(position+1));
		viewHolder.mScheduleGridAdapter.setLocationPos(position);
	}
	
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
		View convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycleview_schedule_item, viewGroup,false);
		ViewHolder viewHolder = new ViewHolder(convertView,mContext,mOnItemClickListener);
		mViewHolders.add(viewHolder);
		return viewHolder;
	}
	
	public static class ViewHolder extends RecyclerView.ViewHolder{
		public GridView mGridView;
		public TextView mTvTime;
		public TextView mTvNum;
		public ScheduleGridAdapter mScheduleGridAdapter;
		ViewHolder(View view,Context context,OnItemClickListener onItemClickListener) {
			super(view);
			mGridView = (GridView) view.findViewById(R.id.gv_schedule);
			mScheduleGridAdapter = new ScheduleGridAdapter(context);
			mGridView.setAdapter(mScheduleGridAdapter);
			mGridView.setOnItemClickListener(onItemClickListener);
			mTvTime = (TextView) view.findViewById(R.id.tv_time);
			mTvNum = (TextView) view.findViewById(R.id.tv_num);
		}
		
	}

	class GridOnItemClickListener implements OnItemClickListener{
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view,
				int position, long id) {
			for(int i = 0,length = mViewHolders.size();i < length;i++){
				if(parent == mViewHolders.get(i).mGridView){
					View par = mViewHolders.get(i).mGridView.getChildAt(position);
					TextView tv = (TextView) par.findViewById(R.id.tv_class_description);
					String q = WEEKDAYS[position];
					ClassDevOpenHelper helper = new ClassDaoMaster.ClassDevOpenHelper(mContext,"class-schedule-db",null);
					SQLiteDatabase db = helper.getReadableDatabase();
					ClassDaoMaster daoMaster = new ClassDaoMaster(db);
					ClassDaoSession daoSession = daoMaster.newSession();
					ClassScheduleDao dao = daoSession.getClassScheduleDao();
					QueryBuilder<ClassSchedule> qb = dao.queryBuilder();
					qb.where(Properties.SelectedTime.like("%"+q+"%"),Properties.ClassName.eq(tv.getText().toString()));
					List<ClassSchedule> cList = qb.list();
					if(cList.size() > 0){
						AddOrModifyClassFragment fragment = AddOrModifyClassFragment.newInstance(mClassScheduleFragment,cList.get(0));
						MainActivity mainActivity = ((MainActivity)mContext);
						mainActivity.addFragment(mainActivity.getSupportFragmentManager(),fragment,fragment.getFragmentTag(),true);
					}
				}
			}			
		}
	}
	
	public static  class ScheduleGridAdapter extends BaseAdapter{
		private int mSelectedPos = -1;
		private int mLocationPos = -1;
		
		private Context mContext;
		public ScheduleGridAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			return 7;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}				

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gridview_shedule_item, parent,false);
				viewHolder.tvDescri = (TextView) convertView.findViewById(R.id.tv_class_description);
				viewHolder.rlShow = (RelativeLayout) convertView.findViewById(R.id.rl_show);
				viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			//查询数据库sqlite
			String q = WEEKDAYS[position];
			ClassDevOpenHelper helper = new ClassDaoMaster.ClassDevOpenHelper(mContext,"class-schedule-db",null);
			SQLiteDatabase db = helper.getReadableDatabase();
			ClassDaoMaster daoMaster = new ClassDaoMaster(db);
			ClassDaoSession daoSession = daoMaster.newSession();
			ClassScheduleDao dao = daoSession.getClassScheduleDao();
			QueryBuilder<ClassSchedule> qb = dao.queryBuilder();
			qb.where(Properties.SelectedTime.like("%"+q+"%"));
			List<ClassSchedule> cList = qb.list();
			int start = -1;
			int end = -1;
			ClassSchedule cla = null;
			for (int i = 0; i < cList.size(); i++) {
				cla = cList.get(i);
				start = Integer.parseInt(cla.getSelectedTime().substring(3,4)) - 1;
				end = Integer.parseInt(cla.getSelectedTime().substring(5,6)) - 1;
			}
			if(mLocationPos >= start && mLocationPos <= end){
				viewHolder.tvDescri.setText(cla.getClassName());				
			}
			
			return convertView;
		}
		
		/**
		 * 重用View的ViewHodler
		 * @author boshao
		 *
		 */
		class ViewHolder{
			TextView tvDescri;
			RelativeLayout rlShow;
			ImageView iv;
		}
		
		public void setSelectedPosition(int position){
			mSelectedPos = position;
		}
		
		/**
		 * 设置当前选择位置
		 * @param pos
		 */
		public void setLocationPos(int pos){
			mLocationPos = pos;
			notifyDataSetChanged();
		}
	}
	
}
